//应付【审核/对账/结算单管理/结算单审核/付款管理/应付管理】详情（零担）
//审核---审核通过、退回办理  查看承运单附件+回单附件
//费用对账 ---修改合同状态  油卡状态  承运单备注   只需要看到回单附件。
//结算单管理- 查看详情 ----查看 回单附件
//结算单审核详情 ----查看 回单附件
//付款管理 ---查看 回单附件
//应付管理  ---查看 查看承运单附件+回单附件

interface feeBreakbulkDetailViewModel extends ng.IScope {
    /**
     * 页面名称
     */
    title: string;
    /**
     * 附件大小
     */
    canvsaWidth: number;
    /**
     * 是否显示附件
     */
    isShowAttachment: boolean;
    /**
     * 驾驶证上传组件
     */
    driverUploader: any;
    /**
     * 挂车证上传组件
     */
    trailerUploader: any;
    /**
     * 行驶证上传组件
     */
    travelUploader: any;
    /**
     * 身份证上传组件
     */
    idCardUploader: any;
    /**
     * 车辆照片
     */
    carPicUploader: any;
     /**
     * 道路运输证
     */
    roadTransportCardUploader:any
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploader:any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploader:any;
    /**
     * 磅单照片
     */
    weightNoteUploader:any;
    /**
     * 回单附件
     */
    totalA: any;

    /**
     * 承运单数据
     */
    carrierInfo: invoiceManagement;
    /**
     * 合同信息
     */
    contractInfo: invoiceManagementContract;
    /**
     * 费用列表信息
     */
    feeListInfo: invoiceManagementFee[];
    /**
     * 点击获取费用详情
     */
    getFeeDetailEvent(data: invoiceManagementFee): void;
    /**
     * 费用信息
     */
    feeInfo: invoiceManagementFee;
    /**
     * 订单总单列表信息
     */
    orderTotalListInfo: invoiceManagementOrder[];
    /**
     * 订单数据
     */
    orderInfo: invoiceManagementOrder;
    /**
     * 订单子单列表信息
     */
    orderChildListInfo: invoiceManagementOrderChild[];
    /**
     * 订单子单详情
     */
    orderChildInfo: invoiceManagementOrderChild;
    /**
     * 点击获取订单数据
     */
    getOrderDetailEvent(data: invoiceManagementOrder): void;
    /**
     * 点击获取子单数据
     */
    getOrderChildDetailEvent(data: invoiceManagementOrderChild): void;

    /**
     * 返回按钮
     */
    backPageEvent(): void;
    /**
     * 回单状态
     */
    receiptStatus: string;
    /**
     * 回单状态枚举下拉
     */
    receiptStatusDropDown: ValueListItemResponse[];
    /**
     * 回单签收日期
     */
    receiptTime: string;
    /**
     * 运费总价
     */
    totalPrice: string;
    /**
     * 申请费用总和
     */
    totalFeePrice: string;

    /**
     *  审核模块///////////////////////////////////////////////////////////
     */
    /**
     * 审核通过btn
     */
    auditBtn: boolean;
    /**
     * 审核通过Event
     */
    auditPassEv(): void;

    /**
     * 审核退回Event
     */
    backAuditEv(): void;

    /**
     * 费用对账模块///////////////////////////////////////////////////////////////
     */
    /**
     * 油卡状态下拉
     */
    oilCCardStatusDropDown: ValueListItemResponse[];

    /**
     * 合同状态下拉
     */
    contractStatusDropDown: ValueListItemResponse[];

    /**
     * 费用对账需更改数据btn-disable
     */
    reconciliationDis: boolean;

    /**
     * 修改合同信息
     */
    editContractData(): void;
    /**
     * 承运单备注
     */
    carryOrderRemarks: string;
    /**
     * 油卡状态
     */
    oilCardStatus: string;
    /**
     * 合同状态
     */
    contractStatus: string;

     /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;
    /**
     * 用以旋转放大的图片的路径
     */
    selectedImgSrc:string;

    attachList:OssUploaderItem[];
}

class feeBreakbulkDetailController {
    /**
     * 承运单ID
     */
    carrierOrderId: string;
    /**
     * 进入页面名
     */
    pageName: string;
    /**
     * 费用ID
     */
    feeId: string;
    name: string;

    uploaderList: AddCarAttachmentDetail[] = [];
    baseImageUrl: string;

    feeStatus: string;

    constructor(private $scope: feeBreakbulkDetailViewModel,  private feeService: IFeeService, private orderService: IorderService,
         private $location: ng.ILocationService, private valueService: valueService, private $http: ng.IHttpService, private $ngBootbox: BootboxService, 
         private $state: angular.ui.IStateService, private routeService: routeService, private commonService: commonService,private ossUploaderService:ossUploaderService) {

        this.feeId = this.$location.search().feeId;

        this.carrierOrderId = this.$location.search().carrierOrderId;
        this.pageName = this.$location.search().pageName;
        this.feeStatus = this.$location.search().feeStatus;

        this.$scope.auditBtn = false;

        this.$scope.canvsaWidth = 800;
        this.name = this.$location.search().name;
        this.$scope.title = "费用对账(零担)";
        if (this.name = "FeeManage") {
            this.$scope.title = "查看详情（零担）";
        }
        this.$scope.isShowAttachment = false;

        this.$scope.reconciliationDis = true;

        this.init();
        
    }
    init(): void {

        this.$scope.feeListInfo = [];

        this.baseImageUrl = this.routeService.getBaseUrl() + "Attachment/";
        // this.baseImageUrl = "http://192.168.0.110/api/Attachment/"
        this.getCarrierData();
        //点击获取数据
        this.$scope.getOrderDetailEvent = this.getOrderDetailEvent;
        this.$scope.getOrderChildDetailEvent = this.getOrderChildDetailEvent;
        this.$scope.getFeeDetailEvent = this.getFeeDetailEvent;
        this.$scope.backPageEvent = this.backPageEvent;

        // this.$scope.backPageEvent = this.backPageEvent;
        this.$scope.backAuditEv = this.backAuditEv;
        this.$scope.editContractData = this.editContractData;

        //加载枚举
        this.loadDropDown();

        //按需显示隐藏
        if (this.pageName === "Audit") {//审核
            this.$scope.auditBtn = true;
            this.$scope.isShowAttachment = true;
            this.initCarrierAttachment();
            this.$scope.auditPassEv = this.auditPassEv;
            this.$scope.backAuditEv = this.backAuditEv;
            if (this.feeStatus != "办理") { this.$scope.auditBtn = false; };
        } else if (this.pageName === "FeeManage") {//应付管理
            this.$scope.canvsaWidth = 400;
            this.$scope.isShowAttachment = true;
            this.initCarrierAttachment();
        } else if (this.pageName === "Checking") {//费用对账
            this.$scope.reconciliationDis = false;
        } else if (this.pageName === "settleManage" || this.pageName === "settleAuditManage" || this.pageName === "settlepay") {//结算单管理--结算单审核详情--付款管理详情

        } else if (this.pageName === "feeManage") {
            this.$scope.canvsaWidth = 400;
            this.$scope.isShowAttachment = true;
            this.initCarrierAttachment();
        }
        this.$scope.handleImgClick=(item)=>{this.$scope.selectedImgSrc=item.imgSrc;}

    }


    /**
     * 公用模块////////////////////////////////////////////////////////
     */

    /**
     * 初始化承运单附件组件
     */
    initCarrierAttachment(): void {
       
        this.$scope.attachList=[
            {
                title:'驾驶证',
                uploader:null,
                uploaderName:'driverUploader'
            },{
                title:'挂车证',
                uploader:null,
                uploaderName:'trailerUploader'
            },{
                title:'行驶证',
                uploader:null,
                uploaderName:'travelUploader'
            },{
                title:'身份证',
                uploader:null,
                uploaderName:'idCardUploader'
            },{
                title:'车辆照片',
                uploader:null,
                uploaderName:'carPicUploader'
            },{
                title:'道路运输证',
                uploader:null,
                uploaderName:'roadTransportCardUploader'
            },{
                title:'道路运输经营许可证',
                uploader:null,
                uploaderName:'roadTransportLicenseUploader'
            },{
                title:'从业资格证',
                uploader:null,
                uploaderName:'occupationalLicenseUploader'
            },{
                title:'磅单照片',
                uploader:null,
                uploaderName:'weightNoteUploader'
            },
        ]
        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.attachList.forEach((attach,index)=>{
            attach.uploader=new OssFile(this.$scope,attach.uploader,{uploaderList:[],tag:index+1});
           
        })
    }


    /**
     * 获取承运单详情
     */
    getCarrierData(): void {
        this.orderService.invoiceManagementDetail(this.carrierOrderId).then((res => {
            this.$scope.carrierInfo = res.data;
            this.$scope.contractInfo = res.data.contract;

            this.$scope.carryOrderRemarks = res.data.remarks;
            this.$scope.oilCardStatus = res.data.contract.oilCardStatusEnum;

            this.feeService.contractDetail(res.data.contract.id).then((res) => {
                this.$scope.contractStatus = res.data.contractStatus;
            })

            // this.$scope.contractStatus = res.data.contract.contractStatus;

            this.$scope.orderTotalListInfo = res.data.orderList;

            var numPrice = 0;
            //申请费用总和
            res.data.feeList.forEach((item, index) => {
                if (item.id === this.feeId) {
                    this.$scope.feeListInfo.push(item)
                }
                numPrice = numPrice + item.feeAmount;
                if (index == res.data.feeList.length - 1) {
                    this.$scope.totalFeePrice = numPrice.toFixed(2);
                }
            })


            //运费总价
            this.$scope.totalPrice = res.data.contract.totalPrice.toFixed(2);
            /**应付单价单位为元/车时 */
            // if (res.data.payablePriceUnitEnum == "1") {
            //     this.$scope.totalPrice = res.data.contract.totalPrice.toFixed(2);
            //     /**应付单价单位不为请选择时 */
            // }else if(res.data.payablePriceUnitEnum != "0"){
            //     this.$scope.totalPrice = (parseFloat(res.data.payablePrice.toString()) * parseFloat(res.data.contract.quantityOfGoods.toString())).toFixed(2);
            // }
            // if (res.data.payablePriceUnitEnum == "1") {
            //     this.$scope.totalPrice = res.data.payablePrice.toFixed(2);
            // } else if (res.data.payablePriceUnitEnum != "0") {
            //     this.$scope.totalPrice = (parseFloat(res.data.payablePrice.toString()) * parseFloat(res.data.contract.quantityOfGoods.toString())).toFixed(2);;
            // }
            if (this.pageName === "Audit" || this.pageName === "feeManage") {
                this.previewCarrierOrderAttachments(res.data.carrierOrderAttachments);
            }
            //加载回单附件
            this.getReceiptAttchment(res.data.orderList);

        }));
    }

    /**
     * 点击获取订单数据
     */
    getOrderDetailEvent = (data: invoiceManagementOrder) => {
        this.$scope.orderInfo = data;
        this.$scope.orderChildListInfo = data.childList;
        this.getReceiptStatus(data.id);
    }

    /**
     * 点击获取子单数据
     */
    getOrderChildDetailEvent = (data: invoiceManagementOrderChild) => {
        this.$scope.orderChildInfo = data;
    }
    /**
     * 点击获取费用详情
     */
    getFeeDetailEvent = (data: invoiceManagementFee) => {
        this.$scope.feeInfo = data;
    }

    loadDropDown(): void {
        this.$scope.receiptStatusDropDown = this.valueService.getReceiptStatusList().data;
        this.$scope.oilCCardStatusDropDown = this.valueService.getOilCCardStatusList().data;
        this.$scope.contractStatusDropDown = this.valueService.getContractStatusList().data;
    }
    /**
     * 获取回单状态
     */
    getReceiptStatus = (id: string) => {
        this.feeService.receiptStatus(id).then(res => {
            this.$scope.receiptStatus = res.data.receiptStatus;//回单状态
            this.$scope.receiptTime = res.data.receiptTime;
        });
    }
    /**
     * 跳转页
     */
    backPageEvent(): void {
        if(this.pageName === "feeManage"){
            this.$state.go('app.fee.feeManagent',{})
        }else if(this.pageName === "Audit"){
            this.$state.go('app.fee.feeAuditList',{name:'feeAuditList'})
        }else{
            window.history.back();
        }
    }


    /**
     * 审核模块///////////////////////////////////////////////////////////////////
     */
    /**
     * 审核通过
     */
    auditPassEv = () => {
        this.$ngBootbox.confirm("确定审核通过吗").then(() => {
            this.feeService.auditedFee(this.feeId).then(res => {
                if(this.pageName === "Audit"){
                    this.$state.go('app.fee.feeAuditList',{name:'feeAuditList'})
                }else{
                    window.history.back();
                }
            });
        });
    }
    /**
     *  审核退回
     */
    backAuditEv = () => {
        this.$ngBootbox.confirm("确定退回办理吗").then(() => {
            this.feeService.returnManage(this.feeId).then(res => {
                if(this.pageName === "Audit"){
                    this.$state.go('app.fee.feeAuditList',{name:'feeAuditList'})
                }else{
                    window.history.back();
                }
            });
        });
    }


    /**
     * 费用对账模块///////////////////////////////////////////////////////////////////
     */
    /**
     * 编辑合同信息-承运单附件
     */
    editContractData = () => {
        this.$ngBootbox.confirm('确定修改合同状态吗?').then(() => {
            this.feeService.carrierOrderRemarks(this.carrierOrderId, this.$scope.carryOrderRemarks).then(res => {
                this.feeService.changeContractStatus(this.$scope.contractInfo.id, this.$scope.oilCardStatus, this.$scope.contractStatus).then(res => {
                    this.$state.go("app.fee.feeCheckList",{name:'feeCheckList'});
                });
            });
        })

    }

    /**
     * 预览承运单附件
     */
    previewCarrierOrderAttachments = (data: attachmentList[]) => {
        //查看承运单附件
        data.forEach((item, index) => {
            this.uploaderList.push(item);
            this.$scope.attachList[Number(item.attachmentTag)-1].uploader.addByAttach(item)
           
        });
    }


    /**
     * 回单附件
     */
    getReceiptAttchment(data: invoiceManagementOrder[]): void {
        this.$scope.totalA = [];
        const OssFile=this.ossUploaderService.getOssFile();
        data.forEach((item, index) => {
            var orderReceiptList = [];
            item.childList.forEach((itemC, indeC) => {
               
                orderReceiptList.push(
                    {
                        title:'回单'+indeC+1,
                        uploader:null,
                        uploaderName:'order'+(index+1)+ 'receipt'+(indeC+1)+'Uploader'
                    }
                )
                orderReceiptList[indeC].uploader=new  OssFile(this.$scope,orderReceiptList[indeC].uploader,{uploaderList:[],tag:indeC+1,multiple:true});

                itemC.receiptAttachments.forEach((itemR, indexR) => {
                    orderReceiptList[indeC].uploader.addByAttach(itemR)
                });
            });
            this.$scope.totalA.push(orderReceiptList);

        });
    };

}
angular.module("tms").controller("feeBreakbulkDetailController", ["$scope", "feeService", "orderService", "$location", "valueService", "$http", "$ngBootbox", "$state", "routeService", "commonService","ossUploaderService", feeBreakbulkDetailController])